package com.pharmacy.controller;


import com.pharmacy.dto.CategoriesAddDto;
import com.pharmacy.dto.CategoriesSelectDto;
import com.pharmacy.dto.CategoriesUpdateDto;
import com.pharmacy.entity.Categories;
import com.pharmacy.result.Result;
import com.pharmacy.service.CategoriesService;
import com.pharmacy.vo.CategoriesSelectVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/categories")
@Slf4j
@Tag(name = "分类管理")
@CrossOrigin
public class CategoriesController {

    @Autowired
    private CategoriesService categoriesService;


    /**
     * 添加分类
     * @param categoriesAddDto
     * @return
     */
    @Operation(summary = "添加分类")
    @PostMapping("/addCategories")
    public Result addCategories(@RequestBody CategoriesAddDto categoriesAddDto) {
        log.info("添加分类{}",categoriesAddDto);
        categoriesService.addCategories(categoriesAddDto);
        return Result.success();
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @Operation(summary = "删除分类")
    @DeleteMapping("/deleteCategories/{id}")
    public Result deleteCategories(@PathVariable Integer id) {
        log.info("删除分类id：{}",id);
        categoriesService.deleteCategories(id);
        return Result.success();
    }

    /**
     * 查询所有分类
     * @return
     */
    @Operation(summary = "查询所有分类")
    @GetMapping("/allSelectCategories")
    public Result<List<CategoriesSelectVo>> allSelectCategories(CategoriesSelectDto categoriesSelectDto) {

        List<CategoriesSelectVo> list = categoriesService.allSelectCategories(categoriesSelectDto);

        return Result.success(list);
    }

    /**
     * 修改分类
     * @param categoriesUpdateDto
     * @return
     */
    @Operation(summary = "修改分类")
    @PutMapping("/updateCategories")
    public Result updateCategories(@RequestBody CategoriesUpdateDto categoriesUpdateDto) {
        log.info("修改分类{}",categoriesUpdateDto);
        categoriesService.updateCategories(categoriesUpdateDto);
        return Result.success();
    }

    /**
     * 根据id查询分类
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Categories> getById(@PathVariable Integer id) {
        Categories categories = categoriesService.getById(id);
        return Result.success(categories);
    }

}
